package edu.sfsu.powerrangers.jeopardy.editor;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import edu.sfsu.powerrangers.jeopardy.gamestate.Category;
import edu.sfsu.powerrangers.jeopardy.gamestate.Clue;

public class CategoryPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private JTextField categoryField;
	private JPanel cluePanel;
	
	public CategoryPanel(int count) {
		super(new BorderLayout(4,4));
		JLabel categoryLabel = new JLabel("Category:");
		categoryField = new JTextField();
		JPanel categoryPanel = new JPanel(new BorderLayout(4,4));
		categoryPanel.add(categoryLabel, BorderLayout.LINE_START);
		categoryPanel.add(categoryField, BorderLayout.CENTER);
		cluePanel = new JPanel(new GridLayout(0,1,4,4));
		while (count-->0) {
			cluePanel.add(new CluePanel());
		}
		add(categoryPanel, BorderLayout.PAGE_START);
		add(cluePanel, BorderLayout.CENTER);
	}
	
	public int getClueCount() {
		return cluePanel.getComponentCount();
	}
	
	public CluePanel getClue(int i) {
		return (CluePanel)cluePanel.getComponent(i);
	}
	
	public void addClue(CluePanel clue) {
		cluePanel.add(clue);
		cluePanel.validate();
	}
	
	public void addClue(int index, CluePanel clue) {
		cluePanel.add(clue, index);
		cluePanel.validate();
	}
	
	public void addClue(CluePanel clue, int index) {
		cluePanel.add(clue, index);
		cluePanel.validate();
	}
	
	public void removeClue(int index) {
		cluePanel.remove(index);
		cluePanel.validate();
	}
	
	public void removeClue(CluePanel clue) {
		cluePanel.remove(clue);
		cluePanel.validate();
	}
	
	public void moveClueUp(int index) {
		CluePanel clue = (CluePanel)cluePanel.getComponent(index);
		cluePanel.remove(index);
		index--;
		if (index < 0) {
			index = 0;
		}
		cluePanel.add(clue, index);
		cluePanel.validate();
	}
	
	public void moveClueDown(int index) {
		CluePanel clue = (CluePanel)cluePanel.getComponent(index);
		cluePanel.remove(index);
		index++;
		if (index > cluePanel.getComponentCount()) {
			index = cluePanel.getComponentCount();
		}
		cluePanel.add(clue, index);
		cluePanel.validate();
	}
	
	public void setClueCount(int count) {
		while (cluePanel.getComponentCount() > count) {
			cluePanel.remove(count);
		}
		while (cluePanel.getComponentCount() < count) {
			cluePanel.add(new CluePanel());
		}
		cluePanel.validate();
	}
	
	public Category getCategory(List<Integer> values) {
		Category c = new Category(categoryField.getText());
		Component[] comps = cluePanel.getComponents();
		for (int i = 0; i < comps.length; i++) {
			if (i >= values.size()) {
				if (values.isEmpty()) {
					values.add(100);
					values.add(200);
				} else if (values.size() == 1) {
					values.add(values.get(0) * 2);
				} else {
					int a = values.get(values.size()-2);
					int b = values.get(values.size()-1);
					values.add(b+b-a);
				}
			}
			c.add(((CluePanel)comps[i]).getClue(values.get(i)));
		}
		return c;
	}
	
	public void setCategory(Category c) {
		categoryField.setText(c.getCategoryName());
		cluePanel.removeAll();
		for (Clue clue : c) {
			CluePanel cp = new CluePanel();
			cp.setClue(clue);
			cluePanel.add(cp);
		}
		cluePanel.validate();
	}
}
